package com.shuoyue.ycgk.ui.papergroups.freedomnew;

import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.FreedomGroup;
import com.shuoyue.ycgk.entity.FreedomPaper;
import com.shuoyue.ycgk.entity.FreedomSet;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.ui.questionbank.practice.freedom.FreedomQuestionActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FreedomGroupInfoActivity extends BaseMvpActivity<FreedomSetContract.Presenter>
        implements FreedomSetContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.setNum)
    TextView setNum;
    @BindView(R.id.questionTotal)
    TextView questionTotal;
    @BindView(R.id.recomend_recycleView)
    RecyclerView recyclerView;
    int modelId;
    FreedomAdapter freedomAdapter;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshlayout;

    public static void start(Context sourse, int modelId) {
        sourse.startActivity(new Intent(sourse, FreedomGroupInfoActivity.class)
                .putExtra("id", modelId));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_freedom_paper_set_info;
    }

    @Override
    protected void initView() {
        pageTitle.setText("自由组卷");

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getFreedomPaper(modelId);
            refreshLayout.finishRefresh(2000);
        });

        freedomAdapter = new FreedomAdapter(null);
        recyclerView.setAdapter(freedomAdapter);
        freedomAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            FreedomPaper paperSet = (FreedomPaper) adapter.getItem(position);
            mPresenter.createFreedomPaper(modelId, paperSet.getType(), paperSet.getTypeName());
        });

        registEmptyView(freedomAdapter);

    }

    @Override
    public void initData() {
        modelId = getIntent().getIntExtra("id", -1);
        mPresenter = new FreedomSetContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getFreedomPaper(modelId);
    }


    @Override
    public void getFreedomPaperSuc(FreedomSet freedomPapers) {
        name.setText(freedomPapers.getFreeName());
        setNum.setText(String.valueOf(freedomPapers.getTotalList()));
        questionTotal.setText(String.valueOf(freedomPapers.getTotalQuestion()));
        freedomAdapter.resetData(freedomPapers.getFreeQuestionSetList());
    }

    @Override
    public void creataPaperSuc(Integer userTestPaper, String name, int type) {
        FreedomQuestionActivity.start(mContext, userTestPaper, name, type != 4);
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

}
